package com.stone.framework.user.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.stone.framework.core.context.UserContext;
import com.stone.framework.core.exception.BusinessException;
import com.stone.framework.message.PageReqMessage;
import com.stone.framework.message.PageResMessage;
import com.stone.framework.message.ReqMessage;
import com.stone.framework.message.ResMessage;
import com.stone.framework.message.user.LoginInfoReqBody;
import com.stone.framework.message.user.LoginInfoResBody;
import com.stone.framework.message.user.TokenInfoReqBody;
import com.stone.framework.message.user.TokenInfoResBody;
import com.stone.framework.message.user.UserDelReqBody;
import com.stone.framework.message.user.UserPageReqBody;
import com.stone.framework.message.user.UserPageResBody;
import com.stone.framework.message.user.UserPasswordReqBody;
import com.stone.framework.message.user.UserReqBody;
import com.stone.framework.user.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "用户")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户登录信息", httpMethod = "POST")
    @PostMapping(value = "/getLoginInfo")
    ResMessage<LoginInfoResBody> getLoginInfo(@Valid @RequestBody ReqMessage<LoginInfoReqBody> reqMessage) {
        LoginInfoResBody resBody = userService.queryOne(reqMessage.getBody());
        return ResMessage.success(resBody);
    }

    @ApiOperation(value = "用户token信息", httpMethod = "POST")
    @PostMapping(value = "/getTokenInfo")
    ResMessage<TokenInfoResBody> getTokenInfo(@Valid @RequestBody ReqMessage<TokenInfoReqBody> reqMessage) {
        TokenInfoResBody resBody = userService.queryTokenInfo(reqMessage.getBody());
        return ResMessage.success(resBody);
    }

    @ApiOperation(value = "分页查询", httpMethod = "POST")
    @PostMapping(value = "/getPage")
    PageResMessage<UserPageResBody> getPage(@Valid @RequestBody PageReqMessage<UserPageReqBody> reqMessage) {
        IPage<UserPageResBody> page = userService.queryPage(reqMessage.getBody(), reqMessage.buildPage());
        return PageResMessage.success(page);
    }

    @ApiOperation(value = "新增", httpMethod = "POST")
    @PostMapping(value = "/add")
    ResMessage<Void> add(@Valid @RequestBody ReqMessage<UserReqBody> reqMessage) {
        userService.save(reqMessage.getBody());
        return ResMessage.success();
    }

    @ApiOperation(value = "修改", httpMethod = "POST")
    @PostMapping(value = "/update")
    ResMessage<Void> update(@Valid @RequestBody ReqMessage<UserReqBody> reqMessage) {
        userService.modify(reqMessage.getBody());
        return ResMessage.success();
    }

    @ApiOperation(value = "刪除", httpMethod = "POST")
    @PostMapping(value = "/delete")
    ResMessage<Void> delete(@Valid @RequestBody ReqMessage<UserDelReqBody> reqMessage) {
        userService.delete(reqMessage.getBody());
        return ResMessage.success();
    }

    @ApiOperation(value = "修改密码", httpMethod = "POST")
    @PostMapping(value = "/updatePassword")
    ResMessage<Void> updatePassword(@Valid @RequestBody ReqMessage<UserPasswordReqBody> reqMessage) {
        String username = UserContext.getUsername().orElseThrow(() -> new BusinessException("无法获取登录人信息"));
        if (!reqMessage.getBody().getUsername().equals(username)) {
            throw new BusinessException("非本人不可修改密码");
        }
        userService.modifyPassword(reqMessage.getBody());
        return ResMessage.success();
    }

    @ApiOperation(value = "重置密码", httpMethod = "POST")
    @PostMapping(value = "/resetPassword")
    ResMessage<Void> resetPassword(@Valid @RequestBody ReqMessage<UserPasswordReqBody> reqMessage) {
        userService.modifyPassword(reqMessage.getBody(), false);
        return ResMessage.success();
    }

}
